﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Teenio.Workflow
{
    public partial class SocialList : BaseControl
    {
        private readonly string MENUNAME = string.Empty;

        public SocialList()
        {
        }

        public SocialList(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblCommandName.Text = MENUNAME;
                BindDataList();
            }
        }

        protected void dtgUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var curData = (TeenioComment) e.Item.DataItem;
                var lblSTT = (Label) e.Item.FindControl("lblSTT");
                var lblCONTENT = (Label) e.Item.FindControl("lblCONTENT");
                lblSTT.Text = curData.ID.ToString();
                lblCONTENT.Text = curData.MESSAGE;
            }
        }

        protected void dtgUsers_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int xID = Convert.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "publish":
                    ManagerFactory.DAOEntity.Update(new TeenioComment
                                                                {
                                                                    ID = xID,
                                                                    STATUS = Convert.ToInt32(chkTinluutru.Checked)
                                                                });
                    BindDataList();
                    break;
                default:
                    break;
            }
        }

        protected void chkTinluutru_CheckedChanged(object sender, EventArgs e)
        {
            BindDataList();
        }

        private void BindDataList()
        {
            List<TeenioComment> lstComment = ManagerFactory.DAOEntity.GetList(new TeenioComment() { STATUS = Convert.ToInt32(!chkTinluutru.Checked) });
            if (lstComment != null)
            {
                dtgUsers.DataSource = lstComment;
                dtgUsers.DataBind();
            }
            else
            {
                dtgUsers.DataSource = null;
                dtgUsers.DataBind();
            }
        }
    }
}